package com.example.uniappspringbootstartweb.controller;

import com.example.uniappspringbootstartweb.entity.Comments;
import com.example.uniappspringbootstartweb.entity.Content;
import com.example.uniappspringbootstartweb.entity.Shares;
import com.example.uniappspringbootstartweb.entity.Userlike;
import com.example.uniappspringbootstartweb.service.ContentService;
import com.example.uniappspringbootstartweb.service.UserlikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by lilijava
 * on 2024/12/30
 * <p>
 * on  uni-app-springboot-start-web
 * @author DELL
 */
@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/api/like")
public class LikeController {
    @Autowired
    UserlikeService userlikeService;
    @Autowired
    ContentService contentService;
    @GetMapping ("/")
    public ResponseEntity<List<Content>> getLikeId(Integer userid) {
        List<Userlike> userLikes = userlikeService.getUserlikesByUserid(Long.valueOf(userid));
        // 创建一个列表来存储获取到的内容
        List<Content> contents = new ArrayList<>();
        // 遍历每个 Userlike 对象并获取对应的内容
        for (Userlike userlike : userLikes) {
            Long likeid = userlike.getLikeid();
            Optional<Content> optionalContent = Optional.ofNullable(contentService.getContentById(likeid));
            // 如果找到了内容，则添加到列表中
            optionalContent.ifPresent(contents::add);
        }
        return  ResponseEntity.ok(contents);
    }
    @GetMapping("/total")
    public int getlikeByContentId(Integer contentId) {
        List<Userlike> list=userlikeService.getlikeByContentid(Long.valueOf(contentId));
        int size = list.size();
        return size;
    }
    @GetMapping("/add")
    public int save(Integer contentId) {
//        long a=(Long) session.getAttribute("userId");
        Userlike like= new Userlike(Long.valueOf(contentId), 1l);
        userlikeService.save(like);
        List<Userlike> list=userlikeService.getlikeByContentid(Long.valueOf(contentId));
        int s = list.size();
        return s;
    }

}
